package com.yeyks.controller.member;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.dto.MemberCardEnableOrderDTO;
import com.yeyks.common.dto.member.MemberCardTsOrderDTO;
import com.yeyks.common.param.MemberCardEnableOrderParam;
import com.yeyks.common.param.member.MemberCardTsOrderQueryParam;
import com.yeyks.common.result.member.MemberCardTsOrderResult;
import com.yeyks.common.result.order.MemberCardEnableOrderDownLoadResult;
import com.yeyks.common.utils.excel.ExcelUtil;
import com.yeyks.commonReference.service.vip.MemberCardEnableOrderService;
import com.yeyks.vip.dal.domain.MemberCardSellRelationLevel;
import com.yeyks.vip.service.MemberCardSellRelationLevelService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * vip卡支付订单表
 *
 * @author shuai
 */
@Api(tags = "V4.3 VIP卡支付订单")
@RestController
@RequestMapping("/mini/vip/pay/order")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MemberCardEnableOrderController {

    private final MemberCardEnableOrderService memberCardEnableOrderService;

    private final MemberCardSellRelationLevelService memberCardSellRelationLevelService;

    @ApiIgnore
    @ApiOperation(value = "v4.1 --> 订单列表", response = MemberCardEnableOrderDTO.class)
    @PostMapping(value = "/list")
    public AjaxResponseBody<PageResult<MemberCardEnableOrderDTO>> selectPageList(MemberCardEnableOrderParam param) {
        PageResult<MemberCardEnableOrderDTO> result = memberCardEnableOrderService.myList(param);
        return AjaxResponseBody.success(result);
    }

    @ApiIgnore
    @ApiOperation(value = "v4.1 --> 导出")
    @PostMapping(value = "/download")
    public void download(HttpServletResponse response, MemberCardEnableOrderParam param) {
        List<MemberCardEnableOrderDownLoadResult> result = memberCardEnableOrderService.download(param);
        ExcelUtil.disposeWriteExcelWithSheets(response, result, new MemberCardEnableOrderDownLoadResult());
    }

    @NeedToken
    @ApiOperation(value = "v4.3 --> 分页查询[优享VIP推手]订单信息", response = MemberCardTsOrderDTO.class)
    @PostMapping("/query/ts")
    public AjaxResponseBody<PageResult<MemberCardTsOrderDTO>> queryTsPage(MemberCardTsOrderQueryParam param){

        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isFalse(loginUser.getType() == AccountType.Administrator, "没有权限访问");
        PageResult<MemberCardTsOrderDTO> pageResult = memberCardEnableOrderService.queryTsPage(param);
        return AjaxResponseBody.success(pageResult);
    }

    @NeedToken
    @ApiOperation(value = "v4.3 --> [优享VIP推手]订单  确认收货")
    @PostMapping("/commit/ts/status")
    public AjaxResponseBody commitGiftStatus(String orderNo){
        Arguments.strIsNull(orderNo, "订单号不能为空");
        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isFalse(loginUser.getType() == AccountType.Administrator, "没有权限访问");
        boolean update = memberCardSellRelationLevelService.update(
                new MemberCardSellRelationLevel().setGiftStatus(1),
                new QueryWrapper<MemberCardSellRelationLevel>()
                        .eq(MemberCardSellRelationLevel.ORDER_NO, orderNo));
        return AjaxResponseBody.success(update);
    }

    @ApiOperation(value = "v4.3 --> [优享VIP推手]订单导出")
    @PostMapping(value = "/ts/download")
    public void tsDownload(HttpServletResponse response, MemberCardTsOrderQueryParam param) {
        List<MemberCardTsOrderResult> result = memberCardEnableOrderService.tsDownload(param);
        ExcelUtil.disposeWriteExcelWithSheets(response, result, new MemberCardTsOrderResult());
    }

}
